<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所在部门" prop="deptId">
        <treeselect style="width: 200px;" v-model="queryParams.deptId" :options="deptOptions"
                    :show-count="true"
                    placeholder="请选择归属部门"/>
      </el-form-item>
      <el-form-item label="用户姓名" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入用户姓名" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="登录账号" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入登录账号" clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号码" clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table border stripe v-loading="loading" :data="wechatUserList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column width="80" show-overflow-tooltip label="编号" align="center" prop="id"/>
      <el-table-column label="姓名" show-overflow-tooltip width="130" align="center" prop="nickName"/>
      <el-table-column label="所在部门" show-overflow-tooltip width="130" align="center" prop="deptName"/>
      <el-table-column label="登录账号" width="130" align="center" prop="userName"/>
      <el-table-column label="手机号码" width="130" align="center" prop="phoneNumber"/>
      <el-table-column label="电子邮箱" show-overflow-tooltip width="170" align="center" prop="email"/>
      <el-table-column label="积分" align="center" width="110" prop="grade">
        <template slot-scope="scope">
          <div @click="handlerGrade(scope.row)" style="color: red;font-weight: bold;text-decoration: underline #FF0000">{{ scope.row.grade }}</div>
        </template>
      </el-table-column>
      <el-table-column label="最后登录时间" align="center" prop="lastLoginTime" width="160"/>
      <el-table-column width="130" label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column width="100" label="用户编号" align="center" prop="userId"/>
      <el-table-column label="openid" show-overflow-tooltip align="center" prop="openid"/>
      <el-table-column label="sessionKey" show-overflow-tooltip width="150" align="center" prop="sessionKey"/>


      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          &lt;!&ndash;          <el-button&ndash;&gt;-->
      <!--          &lt;!&ndash;            size="mini"&ndash;&gt;-->
      <!--          &lt;!&ndash;            type="text"&ndash;&gt;-->
      <!--          &lt;!&ndash;            icon="el-icon-edit"&ndash;&gt;-->
      <!--          &lt;!&ndash;            @click="handleUpdate(scope.row)"&ndash;&gt;-->
      <!--          &lt;!&ndash;            v-hasPermi="['wechatUser:wechatUser:edit']"&ndash;&gt;-->
      <!--          &lt;!&ndash;          >修改&ndash;&gt;-->
      <!--          &lt;!&ndash;          </el-button>&ndash;&gt;-->
      <!--          &lt;!&ndash;          <el-button&ndash;&gt;-->
      <!--          &lt;!&ndash;            size="mini"&ndash;&gt;-->
      <!--          &lt;!&ndash;            type="text"&ndash;&gt;-->
      <!--          &lt;!&ndash;            icon="el-icon-delete"&ndash;&gt;-->
      <!--          &lt;!&ndash;            @click="handleDelete(scope.row)"&ndash;&gt;-->
      <!--          &lt;!&ndash;            v-hasPermi="['wechatUser:wechatUser:remove']"&ndash;&gt;-->
      <!--          &lt;!&ndash;          >删除&ndash;&gt;-->
      <!--          &lt;!&ndash;          </el-button>&ndash;&gt;-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="gradeTitle" :visible.sync="gradeOpen" width="1200px" append-to-body>
      <grade-record ref="gradeRecord"/>

    </el-dialog>

    <!-- 添加或修改微信小程序用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入${comment}"/>
        </el-form-item>
        <el-form-item label="${comment}" prop="sessionKey">
          <el-input v-model="form.sessionKey" placeholder="请输入${comment}"/>
        </el-form-item>
        <el-form-item label="${comment}" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入${comment}"/>
        </el-form-item>
        <el-form-item label="${comment}" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入${comment}"/>
        </el-form-item>
        <el-form-item label="最后登录时间" prop="lastLoginTime">
          <el-date-picker clearable
                          v-model="form.lastLoginTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择最后登录时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入0正常 1删除"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addWechatUser,
  delWechatUser,
  getWechatUser,
  listWechatUser,
  updateWechatUser
} from "@/api/background/wechatUser";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {deptTreeSelect} from "@/api/system/user";
import GradeRecord from "@/views/applet/gradeRecord/index.vue";

export default {
  dicts: ['sys_normal_disable'],
  name: "WechatUser",
  components: {GradeRecord, Treeselect},
  data() {
    return {
      deptOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信小程序用户表格数据
      wechatUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      gradeTitle: '',
      gradeOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openid: null,
        sessionKey: null,
        nickName: null,
        phoneNumber: null,
        deptId: null,
        userName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        openid: [
          {required: true, message: "$comment不能为空", trigger: "blur"}
        ],
        sessionKey: [
          {required: true, message: "$comment不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "0正常 1停用不能为空", trigger: "change"}
        ],
        delFlag: [
          {required: true, message: "0正常 1删除不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getDeptTree()
    this.getList();
  },
  methods: {


    handlerGrade(row) {
      if (row.userId == null) {
        this.$modal.msgError("此微信未绑定员工账号！")
        return false
      }

      this.gradeTitle = row.deptName + ' ' + row.nickName + ' 积分记录'
      this.gradeOpen = true
      this.$nextTick(() => {
        this.$refs.gradeRecord.queryParams.userId = row.userId
        this.$refs.gradeRecord.resetQuery()

      })
    },


    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询微信小程序用户列表 */
    getList() {
      this.loading = true;
      listWechatUser(this.queryParams).then(response => {
        this.wechatUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        openid: null,
        sessionKey: null,
        userId: null,
        createTime: null,
        phoneNumber: null,
        lastLoginTime: null,
        status: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信小程序用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWechatUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微信小程序用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWechatUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWechatUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除微信小程序用户编号为"' + ids + '"的数据项？').then(function () {
        return delWechatUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wechatUser/wechatUser/export', {
        ...this.queryParams
      }, `wechatUser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
